package com.bonc.rms.controller;

import java.util.List;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bonc.rms.entity.UserEntity;
import com.bonc.rms.service.LoginService;
import com.bonc.rms.service.QueryService;

/**
 * 登录控制器
 * 
 * @author FlyingKiller
 */
@Controller(value = "loginController")
public class LoginController {
	
	@Autowired
	LoginService loginService;

	@Autowired
	QueryService queryService;

	/**
	 * 登录
	 * loginTag
	 * 0：无该用户
	 * -1：密码错误
	 * success：登录成功
	 * @param name
	 * @param password
	 * @param checkCode
	 * @param httpSession
	 * @return
	 */

	@RequestMapping(value = "/login")
	public ModelAndView login(@RequestParam(name = "name", required = true) String name,
							  @RequestParam(name = "password", required = true) String password,
							  @RequestParam(name = "checkCode", required = false, defaultValue = "") String checkCode,
							  HttpSession httpSession) {

		// 验证验证码,若正确则继续进行，否则返回登录页面
		// String sessionCheckCode = (String)
		// httpSession.getAttribute("checkCode");
		// if (!sessionCheckCode.equals(checkCode)) {
		// modelAndView = new ModelAndView("login");
		// modelAndView.addObject("loginTag", "checkCodeError");
		// return modelAndView;
		// }
							  
		// 上一步正确则验证用户名和密码
		List<Object> tag = loginService.login(name, password);
		// 用户名和密码均正确则加载首页数据，错误则返回登录页
		if ((int)tag.get(0)==1) {
			// 登录时查询首页搜索结果显示列
			// String before = queryService.queryShowSelect(22);
			ModelAndView modelAndView = new ModelAndView("index");
			UserEntity user = new UserEntity();
			user = (UserEntity) tag.get(1);
			modelAndView.addObject("loginTag", "success");
			modelAndView.addObject("resumeList", loginService.getResume());
			modelAndView.addObject("authority", user.getAuthority());
			httpSession.setAttribute("name", name);
			httpSession.setAttribute("user", user);
			// httpSession.setAttribute("show_select", before);
			return modelAndView;
		} else if((int)tag.get(0)==-1){
			//密码错误
			ModelAndView modelAndView = new ModelAndView("login");
			modelAndView.addObject("loginTag", "error");
			return modelAndView;
		}else{
			//无该用户
			ModelAndView modelAndView = new ModelAndView("login");
			modelAndView.addObject("loginTag", "error");
			return modelAndView;
		}

	}
	
	

	/**
	 * 主页(需要加载数据)
	 */
	
	
	@RequestMapping(value = "/index")
	public ModelAndView indexPage() {
	ModelAndView modelAndView = new ModelAndView("index");
	modelAndView.addObject("resumeList", loginService.getResume());
	modelAndView.addObject("dashbordDataMap", loginService.getDashBoardData());
	return modelAndView;
	}
    
	
	

	/**
	 * 登出
	 */
	@RequestMapping(value = "/logout")
	public ModelAndView logout(HttpSession httpSession) {
		httpSession.invalidate();
		System.out.println(httpSession);
		ModelAndView modelAndView = new ModelAndView("login");
		return modelAndView;
	}
}
